﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using FarmacieDAL;
using System.ComponentModel;
using Telerik.Windows.Controls.Charting;
using Farmacie.Util;

namespace Farmacie.Controls
{
    /// <summary>
    /// Interaction logic for StocCritic.xaml
    /// </summary>
    public partial class StocCritic : UserControl, INotifyPropertyChanged
    {
        private Medicament medicamentSelectat;
        private List<Medicament> toateMedicamentele;
        private double stocEstimat;

        #region Properties

        public List<Medicament> ToateMedicamentele
        {
            get { return toateMedicamentele; }
        }

        public Medicament MedicamentSelectat
        {
            get { return medicamentSelectat; }
            set
            {
                if (value != medicamentSelectat)
                {
                    medicamentSelectat = value;
                    OnPropertyChanged("MedicamentSelectat");
                    buttonCalculeaza.IsEnabled = medicamentSelectat != null;
                    buttonUpdate.IsEnabled = medicamentSelectat != null;
                }
            }
        }

        public double StocEstimat
        {
            get { return stocEstimat; }
            set
            {
                if (value != stocEstimat)
                {
                    stocEstimat = value;
                    OnPropertyChanged("StocEstimat");
                }
            }
        }

        #endregion

        public StocCritic()
        {
            InitializeComponent();
            toateMedicamentele = MedicamenteManager.ToateMedicamentele;
            buttonCalculeaza.Click += buttonCalculeaza_Click;
            buttonUpdate.Click += new RoutedEventHandler(buttonUpdate_Click);
            this.DataContext = this;
        }

        void buttonUpdate_Click(object sender, RoutedEventArgs e)
        {
            var stocCritic = FarmacieDALManager.EntitiesContext.Stoc_Critic.FirstOrDefault(x => x.Id_Medicament == medicamentSelectat.Id_Medicament);

            if (stocCritic != null)
            {
                stocCritic.Cantitate = StocEstimat;
                FarmacieDALManager.EntitiesContext.SaveChanges();
                MedicamenteManager.Refresh();
                StocCriticManager.Refresh();

                RefreshControl();
            }
        }

        void buttonCalculeaza_Click(object sender, RoutedEventArgs e)
        {
            List<Arhiva> maximeArhive = FarmacieDALManager.EntitiesContext.CalculeazaMaximeInSaptamani(MedicamentSelectat.Id_Medicament, (int)numericCantitate.Value.Value).ToList();
            chartMaxime.SeriesMappings.Clear();

            SeriesMapping seriesMappingsArhive = new SeriesMapping();
            seriesMappingsArhive.SeriesDefinition = new StackedLineSeriesDefinition();
            seriesMappingsArhive.LegendLabel = "Maxime saptamanale";
            seriesMappingsArhive.CollectionIndex = 0;

            ItemMapping imY = new ItemMapping();
            imY.DataPointMember = DataPointMember.YValue;
            imY.FieldName = "Y";

            ItemMapping imX = new ItemMapping();
            imX.DataPointMember = DataPointMember.XValue;
            imX.FieldName = "X";

            seriesMappingsArhive.ItemMappings.Add(imY);
            seriesMappingsArhive.ItemMappings.Add(imX);


            SeriesMapping seriesMappingsVarianta = new SeriesMapping();
            seriesMappingsVarianta.SeriesDefinition = new StackedLineSeriesDefinition();
            seriesMappingsVarianta.LegendLabel = "Variatia stocului critic";

            ItemMapping imVariantaY = new ItemMapping();
            imY.DataPointMember = DataPointMember.YValue;
            imY.FieldName = "Y";

            ItemMapping imVariantaX = new ItemMapping();
            imX.DataPointMember = DataPointMember.XValue;
            imX.FieldName = "X";

            seriesMappingsVarianta.ItemMappings.Add(imY);
            seriesMappingsVarianta.ItemMappings.Add(imX);
            seriesMappingsVarianta.CollectionIndex = 1;

            chartMaxime.SeriesMappings.Add(seriesMappingsArhive);
            chartMaxime.SeriesMappings.Add(seriesMappingsVarianta);

            double varianta;

            List<List<Point>> itemsSource = new List<List<Point>>();
            itemsSource.Add(ArhivaGraphicEngine.CalculeazaArhivele(maximeArhive));
            itemsSource.Add(ArhivaGraphicEngine.CalculeazaStocCritic(maximeArhive, out varianta));
            chartMaxime.ItemsSource = itemsSource;

            StocEstimat = maximeArhive.Max(x => x.Cantitate) * medicamentSelectat.Producator.Timp_Livrare + varianta * (1 + (medicamentSelectat.Producator.Timp_Livrare / 7));
        }

        public void RefreshControl()
        {
            MedicamentSelectat = null;
            chartMaxime.ItemsSource = null;
            numericCantitate.Value = 4;
            textStocCriticEstimat.Text = string.Empty;
        }

        #region INotifyPropertyChanged

        public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged(String info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }

        #endregion
    }
}
